<template>
  <div class="collection-page">
    <div class="page-header">
      <h2 class="page-title">我的收藏</h2>
      <div class="collection-count">
        <el-tag type="info" size="large">
          <el-icon><Star /></el-icon>
          共收藏 {{ data.collection.length }} 个房源
        </el-tag>
      </div>
    </div>

    <!-- 收藏列表 -->
    <div class="collection-list" v-if="data.collection.length">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="item in data.collection" :key="item.id">
          <div class="room-card" @click="goToDetail(item.room?.id)">
            <!-- 房间图片 -->
            <div class="image-wrapper">
              <img
                  :src="item.room?.roomImages? item.room.roomImages[0] : '@/assets/imgs/sea.jpg'"
                  class="room-image"
              >
              <div class="room-type-tag">
                <el-tag size="small" type="success" effect="dark">
                  {{ item.room?.roomType || '未知类型' }}
                </el-tag>
              </div>
            </div>

            <!-- 房间信息 -->
            <div class="room-info">
              <h3 class="room-title">{{ item.room?.name || '暂无名称' }}</h3>
              <div class="room-details">
                <div class="price">
                  <span class="price-symbol">￥</span>
                  <span class="price-value">{{ item.room?.price || 0 }}</span>
                  <span class="price-unit">/月 起</span>
                </div>
                <div class="room-actions">
                  <el-button 
                    type="danger" 
                    size="small" 
                    @click.stop="cancelCollection(item.room.id)"
                    class="cancel-btn"
                  >
                    取消收藏
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 空状态 -->
    <div v-else class="empty-state">
      <el-empty description="暂无收藏">
        <template #image>
          <el-icon :size="60"><Star /></el-icon>
        </template>
        <el-button type="primary" @click="$router.push('/front_page/home')">去发现好房</el-button>
      </el-empty>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { Star } from '@element-plus/icons-vue';
import request from "../../utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

const router = useRouter();
const data = reactive({
  user: JSON.parse(localStorage.getItem('localStorageUser')),
  collection: []
});

// 获取收藏列表
const getCollections = async() => {
  request.get('/collection/selectAll', {
    params: {
      userId: data.user.id
    }
  }).then(res => {
    if (res.code === '200') {
      data.collection = res.data;
    }else{
      ElMessage.error('加载收藏数据失败');
    }
  });
};

// 取消收藏
const cancelCollection = async (roomId) => {
  try {
    await ElMessageBox.confirm('确定要取消收藏该房源吗？', '取消收藏确认', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    });

    const res = await request.delete('/collection/delete', {
      params: {
        roomId: roomId,
        userId: data.user.id
        }
    });

    if (res.code === '200') {
      ElMessage.success('取消收藏成功');
      getCollections(); // 重新加载收藏列表
    } else {
      ElMessage.error(res.msg || '取消收藏失败');
    }
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败');
    }
  }
};

getCollections();

// 跳转到房间详情
const goToDetail = (roomId) => {
  // router.push(`/room_detail/${roomId}`);
  router.push({ name: 'room_detail', params: { roomId: roomId } });
};

</script>

<style scoped>
.collection-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  min-height: calc(100vh - 60px);
  background-color: #f5f7fa;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 20px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
}

.collection-count {
  display: flex;
  align-items: center;
  gap: 8px;
}

.collection-count .el-icon {
  font-size: 18px;
  color: #409EFF;
}

.room-card {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  position: relative;
}

.room-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.image-wrapper {
  position: relative;
  width: 100%;
  padding-top: 75%;
  overflow: hidden;
}

.room-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.room-card:hover .room-image {
  transform: scale(1.05);
}

.room-type-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1;
}

.room-info {
  padding: 16px;
}

.room-title {
  font-size: 16px;
  font-weight: 500;
  color: #2c3e50;
  margin-bottom: 12px;
  line-height: 1.4;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.room-details {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  display: flex;
  align-items: baseline;
}

.price-symbol {
  color: #ff4d4f;
  font-size: 14px;
  margin-right: 2px;
}

.price-value {
  color: #ff4d4f;
  font-size: 20px;
  font-weight: bold;
}

.price-unit {
  color: #999;
  font-size: 14px;
  margin-left: 4px;
}

.room-actions {
  display: flex;
  gap: 8px;
}

.cancel-btn {
  padding: 6px 12px;
  font-size: 12px;
}

.empty-state {
  padding: 60px 0;
  text-align: center;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.empty-state .el-icon {
  color: #909399;
}

@media (max-width: 768px) {
  .collection-page {
    padding: 16px;
  }

  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }

  .room-card {
    margin-bottom: 16px;
  }

  .room-title {
    font-size: 14px;
    height: 40px;
  }

  .price-value {
    font-size: 18px;
  }
}
</style>